﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;

namespace CERT.AcceptanceTests.Features
{
    [Binding]
    public class AddTeamDetailSteps
        : TestFixtureBase
    {

        private int TeamMemberCount { get; set; }

        [Given(@"I have selected a team")]
        public void WhenIHaveSelectedATeam(Table table1)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");

            Assert.AreEqual(2, CurrentDriver.FindElementsByName("UserName").Count);

            foreach (var row in table1.Rows)
            {
                var textField = CurrentDriver.FindElementByName(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);
            }

            var loginButton = CurrentDriver.FindElementByName("Login");

            if (loginButton == null)
                Assert.Fail("Expected to find a button with the value of 'Login'.");

            loginButton.Click();
            this.WaitUpTo(500, "Login Failed");

            var navBar = CurrentDriver.FindElementById("main-content");
            if (navBar == null)
                Assert.Fail("Expected to find the navbar");

            this.WaitUpTo(500, "Team load failed");
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Teams");
            
            this.WaitUpTo(500, "Team load failed");

            var teams = CurrentDriver.FindElementsById("teamtitle");
            this.WaitUpTo(50, "Team Member pull failed");
            var team = teams.Skip(1).First(a => a.TagName == "a");
            team.Click();

            this.WaitUpTo(500, "Team Member pull failed");


        }

        [Given("Added a new team member")]
        public void GivenAddedANewTeamMember()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            //TeamMemberCount
            var cnt = CurrentDriver.FindElementsById("editName");

            if (cnt != null)
                this.TeamMemberCount = cnt.Count;

            var addTeamMember = CurrentDriver.FindElementById("AddTeamMember");
            if (addTeamMember == null)
                Assert.Fail("Expected to find a button with the value of 'Add'.");

            addTeamMember.Click();

            WaitUpTo(250, "add failed");
        }

        [When("I press save team member")]
        public void WhenIPressSaveTeamMember(Table table2)
        {
            foreach (var row in table2.Rows)
            {
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);
            }


            var addSave = CurrentDriver.FindElementById("addSave");

            if (addSave == null)
                Assert.Fail("Expected to find a button with the value of 'Add'.");

            addSave.Click();
            WaitUpTo(1000, "save failed");


        }
        [Then("the result should be a new team member on the team")]
        public void ThenTheResultShouldBeANewTeamMember()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            var teamMembers = CurrentDriver.FindElementsById("editName");
            if (teamMembers == null)
                Assert.Fail("Expected to find list of teamMembers.");
            else
                Assert.Greater(teamMembers.Count,this.TeamMemberCount);


        }
    }
}
